package net.ib.mtalk.util;

import net.ib.mtalk.R;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;

public class ColorPickerDialog extends Dialog 
{
	public static Activity parent_activity;
    public interface OnColorSelectedListener 
    {
        void colorSelected(int color);
    }

    private OnColorSelectedListener mListener;
    private int mInitialColor;

    public ColorPickerDialog(Activity activity, Context context, OnColorSelectedListener listener, int initialColor )
    {
        super(context);
        
        mListener = listener;
        mInitialColor = initialColor;
        parent_activity = activity;
    }
    
    private ColorSelector		mcsHue;
    private ColorSlider		mcsSat;
    private ColorSlider		mcsLum;

    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        
        setContentView( R.layout.layout_color_picker_dialog );
        setTitle( this.getContext().getResources().getString(R.string.select_color));
        
        mcsHue = (ColorSelector)findViewById( R.id.hue );
        mcsLum = (ColorSlider)findViewById( R.id.brightness );
        mcsSat = (ColorSlider)findViewById( R.id.saturation );

        // have to save percentage of color, not color it's self
        mcsHue.setColorPickerChangedListener( onHueChanged );
        mcsHue.setColorPickerSelectedListener( onClolorSelected );
        
        mcsHue.setInitialColor(mInitialColor);
		mcsSat.setColors( 0xffffffff, mInitialColor );
		mcsLum.setColors( 0xff000000,  mcsSat.getPortionColor() );
		
        mcsSat.setSliderColorChangedListener( onSatChanged );
        mcsLum.setSliderColorChangedListener( onLumChanged );
     
    }
    

    private ColorSelector.OnColorSelectedListener onClolorSelected = new ColorSelector.OnColorSelectedListener()
    {
		@Override
		public void colorSelected( int color) 
		{
			mListener.colorSelected(color);
			dismiss();
		}
    };

     private ColorSelector.OnColorChangedListener onHueChanged = new ColorSelector.OnColorChangedListener()
    {
		@Override
		public void colorChanged( int color) 
		{
			mcsSat.setColors( 0xffffffff, color );
			mcsLum.setColors( 0xff000000, mcsSat.getPortionColor() );			
		}
    };
    
     private ColorSlider.OnSliderColorChangedListener onSatChanged = new ColorSlider.OnSliderColorChangedListener() 
    {
		@Override
		public void colorChanged(ColorSlider slider, int color, float portion) 
		{
			mcsLum.setColors( 0xff000000, color );
			mcsHue.setColor(mcsLum.getPortionColor());		
		}
	};
	
    
	private ColorSlider.OnSliderColorChangedListener onLumChanged = new ColorSlider.OnSliderColorChangedListener() 
    {
		@Override
		public void colorChanged(ColorSlider slider, int color, float portion) 
		{
			mcsHue.setColor(color);
		}
	};
	
    
}
